﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TimKiem
{
    public partial class frmTimKhachHangTraLaiHang : Form
    {

        public frmTimKhachHangTraLaiHang()
        {
            InitializeComponent();
        }

        private void Chon()
        {
            try
            {
                if (this.banghi == null)
                {
                    MessageBox.Show("Bạn phải chọn khách hàng.", "Thông báo");
                    return;
                }
                Common.StaticVariable.timkiemkhachhang = banghi;
                this.Close();
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        private string MaKhachHang = string.Empty;
        private void GetRow()
        {
            try
            {
                if (khachhang == null) { return; }
                foreach (var item in khachhang)
                {
                    if (item.MaKhachHang == this.MaKhachHang)
                    {
                        banghi = item;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        private void dgvHienThi_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvHienThi.Rows.Count <= 0)
            { return; }
            MaKhachHang = dgvHienThi.CurrentRow.Cells["MaKhachHang"].Value.ToString();
            this.GetRow();
        }

        private void dgvHienThi_DoubleClick(object sender, EventArgs e)
        {
            this.Chon();
        }
        private Entities.KhachHang[] khachhang = null;
        private Entities.KhachHang banghi = null;
        private void Select()
        {
            try
            {
                khachhang = Common.StaticVariable.InterfaceObject.SelectKH1();
                if (khachhang == null)
                {
                    this.Gridview();
                    return;
                }
                dgvHienThi.DataSource = khachhang;
                this.Gridview();
            }
            catch (Exception)
            {
            }
        }
        private void Gridview()
        {
            try
            {
                if (khachhang == null)
                {
                    Entities.KhachHang[] nv = new Entities.KhachHang[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["MaKhachHang"].Visible = true;
                dgvHienThi.Columns["MaKhachHang"].HeaderText = "Mã khách hàng";
                dgvHienThi.Columns["Ten"].Visible = true;
                dgvHienThi.Columns["Ten"].HeaderText = "Tên khách hàng";
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        private void frmTimKhachHangTraLaiHang_Load(object sender, EventArgs e)
        {
            this.Select();
        }

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        Common.StaticVariable.nhacungcap = null;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Thoat();
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            this.Chon();
        }
    }
}
